package com.assur.multiphotopicker.view;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import com.assur.multiphotopicker.R;
import com.assur.multiphotopicker.adapter.ImageSelectResultAdapter;
import com.assur.multiphotopicker.model.ImageItem;
import com.assur.multiphotopicker.util.CustomConstants;
import com.assur.multiphotopicker.util.IntentConstants;

/**
 * 主界面，显示已选中返回的图片
 * 
 * @author assur
 * 
 */
public class MainActivity extends Activity {
	private GridView mGridView;
	private ImageSelectResultAdapter mAdapter;
	public static List<ImageItem> mDataList = new ArrayList<ImageItem>();

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initData();
		initView();
	}

	private void initData() {
		List<ImageItem> incomingDataList = (List<ImageItem>) getIntent()
				.getSerializableExtra(IntentConstants.EXTRA_IMAGE_LIST);
		if (incomingDataList != null) {
			mDataList.addAll(incomingDataList);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		notifyDataChanged();
	}

	public void initView() {
		mGridView = (GridView) findViewById(R.id.gridview);
		mGridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
		mAdapter = new ImageSelectResultAdapter(this, mDataList);
		mGridView.setAdapter(mAdapter);
		mGridView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (position == getDataSize()) {
					Intent intent = new Intent(MainActivity.this,
							ImageChooseActivity.class);
					intent.putExtra(IntentConstants.EXTRA_CAN_ADD_IMAGE_SIZE,
							CustomConstants.MAX_IMAGE_SIZE);
					startActivityForResult(intent, 0);
				}

			}
		});
	}

	private int getDataSize() {
		return mDataList == null ? 0 : mDataList.size();
	}

	public String getString(String s) {
		String path = null;
		if (s == null)
			return "";
		for (int i = s.length() - 1; i > 0; i++) {
			s.charAt(i);
		}
		return path;
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == 0) {

		} else if (resultCode == 1) {
			List<ImageItem> incomingDataList = (List<ImageItem>) data
					.getSerializableExtra(IntentConstants.EXTRA_IMAGE_LIST);
			if (incomingDataList != null) {
				mDataList.addAll(incomingDataList);
				notifyDataChanged();
				initView();
			}
		}
	}

	private void notifyDataChanged() {
		mAdapter.notifyDataSetChanged();
	}

}